package org.september.pisces.led.controller;

import org.september.pisces.led.entity.Led;
import org.september.pisces.led.entity.LedArea;
import org.september.pisces.led.entity.LedFlash;
import org.september.pisces.led.entity.LedPlan;
import org.september.pisces.led.service.LedShowService;
import org.september.simpleweb.auth.PublicMethod;
import org.september.simpleweb.model.ResponseVo;
import org.september.smartdao.CommonDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

import static org.september.pisces.led.service.LedMemory.*;


@Controller
@RequestMapping("led")
public class LedController {

    @Autowired
    private CommonDao dao;

    @Autowired
    private LedShowService ledShowService;


    /**
     * 每个情报板,有手动发布按钮
     */
    @ResponseBody
    @RequestMapping(LED_publish)
    public ResponseVo<String> publish(Led led) {
        ledShowService.oneLedStart(led);
        return ResponseVo.<String>BUILDER().setData("").setCode(ResponseVo.BUSINESS_CODE.SUCCESS);
    }


    @RequestMapping(LED_ledList)
    public ModelAndView ledList() {
        return new ModelAndView();
    }


    @RequestMapping(LED_flashList)
    public ModelAndView flashList() {
        return new ModelAndView();
    }


    @RequestMapping(LED_addFlash)
    public ModelAndView addFlash() {
        return new ModelAndView();
    }


    @RequestMapping(LED_addPlan)
    public ModelAndView addPlan() {
        ModelAndView mv = new ModelAndView();
        List<LedFlash> flashes = dao.listByExample(new LedFlash());
        mv.addObject("flashes", flashes);
        return mv;
    }

    @RequestMapping(LED_editFlash)
    public ModelAndView editFlash(Long id) {
        ModelAndView mv = new ModelAndView();
        LedFlash flash = dao.get(LedFlash.class, id);
        mv.addObject("flash", flash);
        return mv;
    }


    @RequestMapping(LED_design)
    public ModelAndView ledDesign(Long id) {
        ModelAndView mv = new ModelAndView();
        LedFlash flash = dao.get(LedFlash.class, id);
        mv.addObject("flash", flash);
        return mv;
    }


    @RequestMapping(LED_addLed)
    public ModelAndView addLed() {
        return new ModelAndView();
    }


    @RequestMapping(LED_choosePlan)
    public ModelAndView choosePlan(Long id) {
        ModelAndView mv = new ModelAndView();
        Led led = dao.get(Led.class, id);
        List<LedPlan> plans = dao.listByExample(new LedPlan());
        mv.addObject("plans", plans);
        mv.addObject("led", led);
        return mv;
    }


    @ResponseBody
    @RequestMapping(LED_listLed)
    public ResponseVo<List<Led>> listLed() {
        return ResponseVo.<List<Led>>BUILDER().setData(ledList).setCode(ResponseVo.BUSINESS_CODE.SUCCESS);
    }


    @ResponseBody
    @RequestMapping(LED_listPlan)
    public ResponseVo<List<LedPlan>> listPlan() {
        return ResponseVo.<List<LedPlan>>BUILDER().setData(planList).setCode(ResponseVo.BUSINESS_CODE.SUCCESS);
    }


    @ResponseBody
    @RequestMapping(LED_listFlash)
    public ResponseVo<List<LedFlash>> listFlash() {
        return ResponseVo.<List<LedFlash>>BUILDER().setData(flashList).setCode(ResponseVo.BUSINESS_CODE.SUCCESS);
    }


    /**
     * 预览
     */

    @RequestMapping(LED_viewFlash)
    public ModelAndView viewFlash(Long id) {
        ModelAndView mv = new ModelAndView();
        LedFlash flash = dao.get(LedFlash.class, id);
        mv.addObject("flash", flash);
        List<LedArea> areas = listByFlash(id);
        for (LedArea area : areas) {
            String replace = area.getContent().replace("\n", "<br/>");
            area.setContent(replace);
        }
        mv.addObject("areas", areas);
        return mv;
    }


    @ResponseBody
    @RequestMapping(LED_listArea)
    public ResponseVo<List<LedArea>> listArea(Long id) {
        List<LedArea> list = listByFlash(id);
        return ResponseVo.<List<LedArea>>BUILDER().setData(list).setCode(ResponseVo.BUSINESS_CODE.SUCCESS);
    }


    @ResponseBody
    @RequestMapping(LED_getArea)
    public ResponseVo<LedArea> getArea(Long id) {
        LedArea area = dao.get(LedArea.class, id);
        return ResponseVo.<LedArea>BUILDER().setData(area).setCode(ResponseVo.BUSINESS_CODE.SUCCESS);
    }


    public List<LedArea> listByFlash(Long flashId) {
        LedArea area = new LedArea();
        area.setFlashId(flashId);
        return dao.listByExample(area);
    }





    /* 以下为增删改 **/

    @PublicMethod
    @ResponseBody
    @RequestMapping("doAddLed")
    public ResponseVo<String> doAddLed(Led led) {
        Led example = new Led();
        example.setHost(led.getHost());
        int i = dao.countByExample(example);
        if (i > 0) {
            return ResponseVo.<String>BUILDER().setDesc("ip已存在").setCode(ResponseVo.BUSINESS_CODE.FAILED);
        }
        led.setEnableFlag(0);
        dao.save(led);
        ledList.add(led);
        return ResponseVo.<String>BUILDER().setData("").setCode(ResponseVo.BUSINESS_CODE.SUCCESS);

    }


    @PublicMethod
    @ResponseBody
    @RequestMapping("doAddPlan")
    public ResponseVo<String> doAddPlan(LedPlan plan) {
        dao.save(plan);
        ledShowService.init();
        return ResponseVo.<String>BUILDER().setData("").setCode(ResponseVo.BUSINESS_CODE.SUCCESS);

    }

    //新增一个素材  不影响任何东西

    @PublicMethod
    @ResponseBody
    @RequestMapping("doAddFlash")
    public ResponseVo<String> doAddFlash(LedFlash flash) {
        dao.save(flash);
        LedArea area = new LedArea();
        area.setWidth(flash.getW());
        area.setHeight(flash.getH());
        area.setFlashId(flash.getId());
        dao.save(area);
        flashList.add(flash);
        return ResponseVo.<String>BUILDER().setData("").setCode(ResponseVo.BUSINESS_CODE.SUCCESS);
    }


    @PublicMethod
    @ResponseBody
    @RequestMapping("deleteLed")
    public ResponseVo<String> deleteLed(Long id) {
        Led led = dao.get(Led.class, id);
        if (led == null) {
            return ResponseVo.<String>BUILDER().setDesc("未找到").setCode(ResponseVo.BUSINESS_CODE.FAILED);
        }
        dao.delete(led);
        ledShowService.init();

        return ResponseVo.<String>BUILDER().setData("").setCode(ResponseVo.BUSINESS_CODE.SUCCESS);
    }


    @PublicMethod
    @ResponseBody
    @RequestMapping("changeEnableFlag")
    public ResponseVo<String> changeEnableFlag(Led led) {
        Led led1 = dao.get(Led.class, led.getId());
        if (led1 == null) {
            return ResponseVo.<String>BUILDER().setDesc("未找到").setCode(ResponseVo.BUSINESS_CODE.FAILED);
        }
        led1.setEnableFlag(led.getEnableFlag());
        dao.update(led1);
        ledShowService.init();
        return ResponseVo.<String>BUILDER().setData("").setCode(ResponseVo.BUSINESS_CODE.SUCCESS);


    }

    // 以下不会影响led基本属性


    @PublicMethod
    @ResponseBody
    @RequestMapping("doChoosePlan")
    public ResponseVo<String> doChoosePlan(Led led) {
        Led led1 = dao.get(Led.class, led.getId());
        if (led1 == null) {
            return ResponseVo.<String>BUILDER().setDesc("未找到").setCode(ResponseVo.BUSINESS_CODE.FAILED);
        }
        led.setPlanId(led.getPlanId());
        dao.update(led);
        ledShowService.init();
        return ResponseVo.<String>BUILDER().setData("").setCode(ResponseVo.BUSINESS_CODE.SUCCESS);
    }


//修改一个素材  查出关联的plan


    @PublicMethod
    @ResponseBody
    @RequestMapping("doEditFlash")
    public ResponseVo<String> doEditFlash(LedFlash flash) {
        LedFlash flash1 = dao.get(LedFlash.class, flash.getId());
        if (flash1 == null) {
            return ResponseVo.<String>BUILDER().setDesc("未找到").setCode(ResponseVo.BUSINESS_CODE.FAILED);
        }
        dao.update(flash);
        ledShowService.init();

//        flashList = flashList.stream()
//                .filter(x -> x.getId().equals(flash.getId())).collect(Collectors.toList());
//        flashList.add(flash);
        return ResponseVo.<String>BUILDER().setData("").setCode(ResponseVo.BUSINESS_CODE.SUCCESS);
    }


    @PublicMethod
    @ResponseBody
    @RequestMapping("deleteFlash")
    public ResponseVo<String> deleteFlash(Long id) {
        LedFlash flash = dao.get(LedFlash.class, id);
        if (flash == null) {
            return ResponseVo.<String>BUILDER().setDesc("未找到").setCode(ResponseVo.BUSINESS_CODE.FAILED);
        }
        for (LedPlan plan : planList) {
            List<LedFlash> flashes = plan.getFlashes();
            boolean anyMatch = flashes.stream().anyMatch(x -> x.getId().equals(id));
            if (anyMatch) {
                return ResponseVo.<String>BUILDER().setDesc(plan.getPlanName() + "方案正在使用该素材,无法删除!").setCode(ResponseVo.BUSINESS_CODE.FAILED);
            }
        }
        dao.delete(flash);
        ledShowService.init();

        return ResponseVo.<String>BUILDER().setData("").setCode(ResponseVo.BUSINESS_CODE.SUCCESS);
    }

    // area修改不init

    @PublicMethod
    @ResponseBody
    @RequestMapping("addArea")
    public ResponseVo<String> addArea(Long flashId) {
        LedArea area = new LedArea();
        area.setFlashId(flashId);
        area.setTextAlign("left");
        dao.save(area);
        for (LedFlash flash : flashList) {
            flash.getAreas().add(area);
        }
        return ResponseVo.<String>BUILDER().setData("").setCode(ResponseVo.BUSINESS_CODE.SUCCESS);
    }


    @PublicMethod
    @ResponseBody
    @RequestMapping("deleteArea")
    public ResponseVo<String> deleteArea(Long id) {
        LedArea area = dao.get(LedArea.class, id);
        if (area == null) {
            return ResponseVo.<String>BUILDER().setDesc("未找到").setCode(ResponseVo.BUSINESS_CODE.FAILED);
        }
        dao.delete(area);
        for (LedFlash flash : flashList) {
            List<LedArea> areas = flash.getAreas();
            for (int i = 0; i < areas.size(); i++) {
                LedArea ledArea = areas.get(i);
                if (ledArea.getId().equals(area.getId())) {
                    areas.remove(i);
                    break;
                }
            }
        }

        return ResponseVo.<String>BUILDER().setData("").setCode(ResponseVo.BUSINESS_CODE.SUCCESS);

    }


    @PublicMethod
    @ResponseBody
    @RequestMapping("updateArea")
    public ResponseVo<String> updateArea(LedArea area) {
        LedArea area1 = dao.get(LedArea.class, area.getId());
        if (area1 == null) {
            return ResponseVo.<String>BUILDER().setDesc("未找到").setCode(ResponseVo.BUSINESS_CODE.FAILED);
        }
        dao.update(area);
        for (LedFlash flash : flashList) {
            List<LedArea> areas = flash.getAreas();
            for (int i = 0; i < areas.size(); i++) {
                LedArea ledArea = areas.get(i);
                if (ledArea.getId().equals(area.getId())) {
                    areas.remove(i);
                    areas.add(i, area);
                    break;
                }
            }
        }
        return ResponseVo.<String>BUILDER().setData("").setCode(ResponseVo.BUSINESS_CODE.SUCCESS);
    }



    private static final String Mapping_Prefix = "/led";
    public static final String LED_publish = Mapping_Prefix + "/publish";
    public static final String LED_ledList = Mapping_Prefix + "/ledList";
    public static final String LED_flashList = Mapping_Prefix + "/flashList";
    public static final String LED_listLed = Mapping_Prefix + "/listLed";
    public static final String LED_listPlan = Mapping_Prefix + "/listPlan";
    public static final String LED_listFlash = Mapping_Prefix + "/listFlash";
    public static final String LED_listArea = Mapping_Prefix + "/listArea";
    public static final String LED_addFlash = Mapping_Prefix + "/addFlash";
    public static final String LED_addPlan = Mapping_Prefix + "/addPlan";
    public static final String LED_editFlash = Mapping_Prefix + "/editFlash";
    public static final String LED_design = Mapping_Prefix + "/design";
    public static final String LED_addLed = Mapping_Prefix + "/addLed";
    public static final String LED_choosePlan = Mapping_Prefix + "/choosePlan";
    public static final String LED_viewFlash = Mapping_Prefix + "/viewFlash";
    public static final String LED_getArea = Mapping_Prefix + "/getArea";

}
